﻿Imports System.Data.Linq
Imports System.Linq

Public Class CustomerRepository
    Private dc As New TanNamChinhDataContext

    Public Sub New()
        dc = GeneralClassDAL.dc
    End Sub

    Public Function SelectAllRecords() As IQueryable(Of tblCustomer)
        Dim query = From p In dc.tblCustomers
                    Where p.IsActive = True
                        Select p
        Return query
    End Function

    Public Function SelectRecordById(ByVal id As Integer) As tblCustomer
        Dim query = dc.tblCustomers.SingleOrDefault(Function(p As tblCustomer) p.ID_Customer = id)
        Return query
    End Function

    Public Sub Insert(ByVal obj As tblCustomer)
        dc.tblCustomers.InsertOnSubmit(obj)
        dc.SubmitChanges()
    End Sub

    Public Sub Update(ByVal obj As tblCustomer)
        dc.Refresh(RefreshMode.KeepCurrentValues, obj)
        dc.SubmitChanges(ConflictMode.ContinueOnConflict)
    End Sub

    Public Sub DeleteRecordById(ByVal id As Integer)
        Dim customer = From p In dc.tblCustomers
                   Where p.ID_Customer = id
                   Select p
        dc.tblCustomers.DeleteOnSubmit(customer)
        dc.SubmitChanges()
    End Sub

    Public Function GetCustomerByCompany(ByVal id_company As Integer) As IQueryable(Of tblCustomer)
        Dim query = From p In dc.tblCustomers
                    Where p.ID_Company = id_company And p.IsActive = True
                    Select p
        Return query
    End Function

    'Public Function SelectRecordByName(ByVal isExport As Integer, ByVal CompanyName As String, ByVal CustomerName As String) As tblCustomer
    '    Dim query = (From p In SelectAllCustomers(isExport)
    '                Where p.CustomerName = CustomerName And p.tblCompany.Name = CompanyName
    '                Select p).SingleOrDefault()
    '    Return query
    'End Function
End Class
